// pages/ucenter/index/index.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../utils/user.js');
var app = getApp();
var ID = app.globalData2.userid;
var hostURL = app.globalData1.hostURL;

Page({
  data: {


    userInfo: {
      nickName: '点击登录',
      avatarUrl: 'http://yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png',
    },
    hasLogin: false,
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // console.log(app.globalData2.hasLogin) 
    // console.log(app.globalData2.hasLogin)
  },
  onReady: function () {

  },

  onShow: function () {
    //获取用户的登录信息

    console.log(app.globalData2.hasLogin)

    if (app.globalData2.hasLogin) {
      let userInfo = wx.getStorageSync('userInfo');//wx.getStorageSync(KEY),从本地缓存中同步获取指定 key 对应的内容
      let token = wx.getStorageSync('token');

      this.setData({
        userInfo: userInfo,
        hasLogin: app.globalData2.hasLogin
      });
    }

  },
  //  let userInfo = wx.getStorageSync('userInfo');//wx.getStorageSync(KEY),从本地缓存中同步获取指定 key 对应的内容
  //let token = wx.getStorageSync('token'); 

  // 页面显示
  //  if (userInfo && token) {
  //  app.globalData.userInfo = userInfo;
  //  app.globalData.token = token;
  //}

  // this.setData({
  //  userInfo: app.globalData.userInfo,
  //}); 


  // goLogin() {
  //   if (!app.globalData.hasLogin) {
  //     wx.navigateTo({ url: "/pages/ucenter/login/login" });
  //   }
  // },

  goOrder() {
    if (app.globalData2.hasLogin) {  
      wx.navigateTo({ url: "/pages/order-list/index?userId="+this.data.userInfo.id});
    }
    else {
      wx.navigateTo({ url: "/pages/ucenter/login/login" });
    }
  },
  //waitservice
  //shopfinish
  waitservice() {
     if (app.globalData2.hasLogin) {
        wx.navigateTo({ url: "/pages/waitservice/waitservice?userId=" + this.data.userInfo.id });
     }
     else {
        wx.navigateTo({ url: "/pages/ucenter/login/login" });
     }
  },
  shopfinish() {
     if (app.globalData2.hasLogin) {
        wx.navigateTo({ url: "/pages/shopfinish/shopfinish?userId=" + this.data.userInfo.id });
     }
     else {
        wx.navigateTo({ url: "/pages/ucenter/login/login" });
     }
  },

  goMyself() {
    if (app.globalData2.hasLogin) {
      wx.navigateTo({ url: "/pages/ucenter/myinfo/myinfo" });
    }
    else {
      wx.navigateTo({ url: "/pages/ucenter/login/login" });
    };

  },


  gomypublish() {
    if (app.globalData2.hasLogin) {
      wx.navigateTo({ url: "/pages/ucenter/mypublish/mypublish" });
    }
    else {
      wx.navigateTo({ url: "/pages/ucenter/login/login" });
    };
  },


  exitLogin: function (res) {
    let that = this

    wx.request({
      url: hostURL + 'sharedplatform-web/user/loginOut.action',
      data: ({

      }),
      method: 'Post',
      header: {
        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
      },
      success: function (res) {

        var errno = res.data.errno;
        if (errno == 0) {
          wx.showModal({
            title: '',
            confirmColor: '#b4282d',
            content: '退出登录？',
            success: function (res) {

              try {
                wx.clearStorageSync()
              } catch (e) {
                
              }
              var page = getCurrentPages().pop();
              if (page == undefined || page == null) return;
              page.onLoad()



              if (res.confirm) {
                // wx.removeStorageSync('token');
                // wx.removeStorageSync('userInfo');

                let userInfo =
                  {
                    nickName: '点击登录',
                    avatarUrl: 'http://yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png'
                  }
                app.globalData2.hasLogin = false
                that.setData({
                  userInfo: userInfo,
                  hasLogin: app.globalData2.hasLogin
                })

                wx.switchTab({
                  url: '/pages/ucenter/index/index'
                });
              }
            }
          })

          wx.switchTab({
            url: '/pages/ucenter/index/index'
          });

        }

      }
    })

  },


  goLogin: function () {
    if (!app.globalData2.hasLogin) {
      wx.navigateTo({
        url: '../login/login',
      })
    }
  },
  //扫描二维码
  scanQRcode: function () {
    var that = this;
    var show;
    wx.scanCode({
      success: (res) => {
        //获取二维码中信息
        var result = res.result
        //向后台发送二维码请求
        wx.request({
          url: hostURL + 'sharedplatform-web/user/findUserById.action',
          method: 'post',
          data: {
            user_id: result
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
          },
          success: function (res) {
            console.log("list请求成功")
            var usersend = res.data.userName
            var phone = res.data.phone
            console.log(usersend)
            console.log(phone)

            wx.setStorage({
              key: "usersend",
              data: usersend,
            }),
              wx.setStorage({
                key: "phone",
                data: phone,
              })
            console.log(usersend)
            //跳转
            wx.navigateTo({ url: "../otherInfo/otherInfo" });
          },
        })
        wx.showToast({
          title: wx.getStorageSync('usersend'),
          icon: 'success',
          duration: 2000
        })
      },
      fail: (res) => {
        wx.showToast({
          title: '失败',
          icon: 'success',
          duration: 2000
        })
      },
      complete: (res) => {
      }
    })
  },
  //我的二维码

  myQRcode: function () {
    var userid = wx.getStorageSync('userInfo').id
    console.log(userid)
    if (app.globalData2.hasLogin) {
      wx.request({
        url: hostURL + 'sharedplatform-web/qrCode/findQrCode.action',
        method: 'post',
        data: {
          user_id: userid
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
        },
        success: function (res) {
          if (res.data.errno == 0) {
            console.log("list请求成功")
            var qrPath = res.data.qrUrl
            console.log(qrPath)

            //  wx.setStorage({
            //    key: "qrUrl",
            //    data: qrPath,
            //  })
            console.log(qrPath)
            //跳转
            wx.navigateTo({ url: "../qeCode/qeCode?qrPath=" + qrPath });
          }

        },
      })
    }
    else {
      wx.navigateTo({ url: "/pages/ucenter/login/login" });
    };
  }
})